package com.the_ring.chapter12.widget;

import android.animation.PointFEvaluator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Point;
import android.graphics.PointF;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.the_ring.chapter12.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class RewardView extends RelativeLayout {

    private Context mContext;
    private int mLayoutWidth, mLayoutHeight; // 当前视图的宽度和高度
    private LayoutParams mLayoutParams;     // 打赏礼物的布局参数
    private List<Drawable> mDrawableList = new ArrayList<>(); // 图形列表
    private int dip_35;
    private int[] mDrawableArray = new int[] { R.drawable.gift01, R.drawable.gift02, R.drawable.gift03, R.drawable.gift04, R.drawable.gift05, R.drawable.gift06};

    public RewardView(Context context) {
        this(context, null);
    }

    public RewardView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        for (int drawableId : mDrawableArray) {
            mDrawableList.add(mContext.getDrawable(drawableId));
        }
        dip_35 = dip2px(mContext, 35);
        mLayoutParams = new LayoutParams(dip_35, dip_35);
        // 代码设置李五的起始布局方式，底部居中
        mLayoutParams.addRule(CENTER_HORIZONTAL, TRUE);
        mLayoutParams.addRule(ALIGN_PARENT_BOTTOM, TRUE);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mLayoutWidth = getMeasuredWidth();
        mLayoutHeight = getMeasuredHeight();
    }

    // 添加打赏礼物的视图并播放打赏动画
    public void addGiftView() {
        int pos = new Random().nextInt(mDrawableList.size());
        ImageView imageView = new ImageView(mContext);
        imageView.setImageDrawable(mDrawableList.get(pos));
        imageView.setLayoutParams(mLayoutParams);
        addView(imageView);
        // 创建礼物的漂移动画
        ScaleAnimation scaleAnim = new ScaleAnimation(0.2f, 1.0f, 0.2f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 1.0f);
        scaleAnim.setDuration(500);
        imageView.startAnimation(scaleAnim);
        playBezierAnimation(imageView);     // 播放礼物的漂移动画（贝塞尔曲线）
    }

    // 播放礼物的漂移动画（贝塞尔曲线）
    public void playBezierAnimation(View giftView) {
        // 初始化贝塞尔计算器
        BezierEvaluator evaluator = new BezierEvaluator(getPoint(), getPoint());
        PointF beginPoint = new PointF(mLayoutWidth / 2 - dip_35 / 2, mLayoutHeight - dip_35 / 2);
        float endX = (float) (Math.random() * mLayoutWidth - dip_35 / 2);
        float endY = (float) (Math.random() * 10);
        PointF endPoint = new PointF(endX, endY);
        // 创建一个属性动画
        ValueAnimator animator = ValueAnimator.ofObject(evaluator, beginPoint, endPoint);
        animator.addUpdateListener(animation -> {
            // 获取二次贝塞尔曲线的坐标点，用于指定打赏礼物的当前位置
            PointF point = (PointF) animation.getAnimatedValue();
            giftView.setX(point.x);
            giftView.setY(point.y);
            giftView.setAlpha(1 - animation.getAnimatedFraction());
        });
        animator.setTarget(giftView); // 设置动画的播放目标
        animator.setDuration(3000);
        animator.start();
    }

    // 生称随机控制点
    private PointF getPoint() {
        PointF point = new PointF();
        point.x = (float) (Math.random() * mLayoutWidth - dip_35 / 2);
        point.y = (float) (Math.random() * mLayoutHeight / 5);
        return point;
    }

    // 根据是手机的分辨率从 dp 的单位转化为 px
    public int dip2px(Context context, float dpValue) {
        // 获取手机像素密度（1dp 对应几个 px）
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f); // 四舍五入取整
    }

    public class BezierEvaluator extends PointFEvaluator {
        // TODO 贝塞尔曲线插值器的具体实现

        public BezierEvaluator(PointF startPoint, PointF endPoint) {
        }
    }
}
